/*
	新建邮件模板
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

function DomInit(){

	var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken };

	// wang editor 富文本编辑器
	var WEditor = window.wangEditor, editorHtml = new WEditor('#html');
	editorHtml.create();
	editorHtml.txt.html("尊敬的會員%name%：")

	this.addNewEmailModel = function(data, layer, form){

		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
		var _ajaxData = $.extend(ajaxData, data, {html: editorHtml.txt.html()});

		$.ajax({
			url: BASE_SERVICEURL + URlS.sys_email,
			type: 'PUT',
			data: _ajaxData,
			success: function(responseData){
				layer.close(loadIndex);
				if(responseData.msg.code == 200){
					//console.log(responseData.data)
					layer.msg(responseData.data.msg, {time: 2000})
				}else{
					layer.msg(responseData.msg.msg, {time: 2000})
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000});
			}
		});


	}
}


$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer, form = layui.form;
		
		var dom_init = new DomInit();
		
		//修改
		form.on('submit(add)', function(data){
        	console.log(data)
        	var form_data = {
        		name: data.field.name,
                title: data.field.title,
                admin: adminLoginedInfo.aId
        	};

        	layer.confirm("確認新增郵件範本？", {title:'提示'}, function(index){
				dom_init.addNewEmailModel(form_data, layer, form);
				layer.close(index); //关闭询问框
			});
        	return false;
        })


	})
})
















